import {CREATE, DELETE, LIST, UPDATE} from '@/constants';
import type {RequestData} from '@ant-design/pro-table';
import {request} from 'umi';

export function createCRUD<T>(route: string) {
  return {
    list: (params: PageParams & AnyFields, options?: AnyFields) =>
      request<RequestData<T>>(`${route}${LIST}`, {
        method: 'GET',
        params: params,
        ...(options || {}),
      }),
    add: (data: AnyFields) =>
      request<Record<string, any>>(`${route}${CREATE}`, {
        data,
        method: 'POST',
      }),
    update: (data: AnyFields) =>
      request<Record<string, any>>(`${route}${UPDATE}`, {
        data,
        method: 'POST',
      }),
    remove: (ids: string) =>
      request<Record<string, any>>(`${route}${DELETE}?ids=${ids}`, {
        method: 'GET',
      }),
  };
}
